//导入request.js请求工具
import request from '@/utils/request.js'

// 提供调用登录接口的函数
export const adminLoginService = (loginData) => {
  // 使用URLSearchParams来构建表单数据
  const params = new URLSearchParams();
  params.append('account', loginData.account);  // 确保参数名与后端一致
  params.append('password', loginData.password);
  return request.post('/admins/login', params, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'  // 设置Content-Type为表单格式
    }
  });
}

// 获取管理员详细信息
export const getAdminInfoService = () => {
  return request.get('/admins/getAdminInfo');
}

// 获取所有用户数据
export const getAllUsers = (params) => {
  return request({
    url: '/admins/getUsers',
    method: 'get',
    params
  })
}

// 更新用户状态
export const updateUser = (data) => {
  return request({
    url: '/admins/updateUser',
    method: 'put',
    data
  })
}

// 删除用户
export const deleteUser = (uid) => {
  return request({
    url: `/admins/deleteUser/${uid}`,
    method: 'delete'
  })
}

// 获取用户详情信息
export const getUserDetail = (uid) => {
  return request({
    url: `/admins/getUserDetail/${uid}`,
    method: 'get'
  })
}

// 更新用户审核状态
export const updateUserStatus = (uid, status) => {
  return request({
    url: `/admins/updateUserStatus/${uid}`,
    method: 'put',
    params: { status }
  })
}

// 获取已通过的用户数据
export const getApprovedUsers = (params) => {
  return request({
    url: '/admins/getApprovedUsers',
    method: 'get',
    params
  })
}

// 重置用户密码
export function resetPassword(uid, newPassword) {
  return request({
    url: `/admins/resetPassword/${uid}`,
    method: 'put',
    params: {
      newPassword
    }
  })
}

// 导出用户数据
export function exportUsers(searchText, startDate, endDate, sortType, status, identity, region, affiliation, ethnicity, gender, onlyApproved) {
  const params = new URLSearchParams({
    searchText: searchText || '',
    startDate: startDate || '',
    endDate: endDate || '',
    sortType: sortType || 'asc',
    status: status || '',
    identity: identity || '',
    region: region || '',
    affiliation: affiliation || '',
    ethnicity: ethnicity || '',
    gender: gender || '',
    onlyApproved: onlyApproved || false
  }).toString();

  return request({
    url: `/admins/exportUsers?${params}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  });
}

// 获取用户统计数据
export const getUserStatistics = () => {
  return request({
    url: '/admins/statistics',
    method: 'get'
  })
}

// 获取待审核用户数量
export const getPendingAuditCount = () => {
  return request({
    url: '/admins/pendingAuditCount',
    method: 'get'
  })
}

// 获取用户界别分布
export const getAffiliationDistribution = () => {
  return request({
    url: '/admins/affiliationDistribution',
    method: 'get'
  })
}

// 获取用户注册趋势
export const getRegistrationTrend = (startDate, endDate) => {
  const params = {}
  if (startDate) params.startDate = startDate
  if (endDate) params.endDate = endDate
  
  return request({
    url: '/admins/registrationTrend',
    method: 'get',
    params
  })
}

// 导出用户变化趋势数据
export function exportTrendData(startDate, endDate) {
  const params = new URLSearchParams()
  if (startDate) params.append('startDate', startDate)
  if (endDate) params.append('endDate', endDate)
  
  return request({
    url: `/admins/exportTrendData?${params.toString()}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  })
}

// 获取活动统计数据
export function getActivityStatistics() {
  return request({
    url: '/admins/activities/statistics',
    method: 'get'
  })
}

// 获取活动趋势数据
export function getActivityTrend() {
  return request({
    url: '/admins/activities/trend',
    method: 'get'
  })
}

// 根据部门 ID 获取部门名称（提案）
export const getDepartmentNameById = (id) => {
  return request.get(`/admins/getDepartmentName/${id}`);
}



// 根据部门 ID 获取部门名称（社情民意）
export const getDepartmentNameById2 = (id) => {
  return request.get(`/admins/getDepartmentName2/${id}`);
}

// 更新用户分数
export const updateUserScore = (uid, score) => {
  return request({
    url: `/admins/updateUserScore/${uid}`,
    method: 'put',
    params: { score }
  })
}

// 清零所有用户分数
export const clearAllScores = () => {
  return request({
    url: '/admins/clearAllScores',
    method: 'put'
  })
}


